package model;
import java.awt.Color;
import java.util.List;
import java.util.Iterator;

/**
 * Class Hunter implements Actor
 * 
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class Hunter implements Actor
{
	private static int BULLETS = 4;
	private Field field;
	private Location location;
	private boolean alive;
	private static Color color = Color.BLACK;
	
	/**
	 * make a new Hunter
	 * @param field
	 * @param location
	 */
    public Hunter(Field field, Location location)
    {
        this.field = field;
        setLocation(location);
        alive = true;
    }
    
    /**
     * set the location for the hunter
     * @param newLocation
     */
    public void setLocation(Location newLocation)
    {
    	if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }

    
    /**
     * Make the hunter act
     * @param field The field currently occupied.
     * @param newActor A list to add new Hunters to.
     */
    public void act(List<Actor> newActor)
    {
    	if(isAlive())
    	{
	        Location location = getLocation();  
	        for(int i=0;i<BULLETS;i++)
	        {
	        	shoot(location);
	        }
	        Location newLocation = getField().freeAdjacentLocation(location);
	        setLocation(newLocation);
    	}
    }
    
    /**
     * makes the Hunter shoot at an animal
     * @param location
     * @return null
     */
    private Location shoot(Location location)
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object actor = field.getObjectAt(where);
            if(actor instanceof Animal) {
                Animal animal = (Animal) actor;
                if(animal.isAlive()) { 
                	animal.setDead();
                    return where;
                }
            }
        }
        return null;
    }      
	
    /**
     * return the location
     * @return location
     */
	public Location getLocation() {
		return location;
	}

	/**
	 * return the field
	 * @return field
	 */
	public Field getField() {
		return field;
	}

	/**
	 * return a boolean
	 * @return boolean to see if Hunter is alive
	 */
	public boolean isAlive()
    {
        return alive;
    }

	/**
	 * Set the Hunter to dead
	 */
	public void setDead()
    {
        alive = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }

	@Override
	/**
	 * overrides the getColor method
	 * @return color
	 */
	public Color getColor() {
		return color ;
	}
}

